package com.flong.selenium;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

@SuppressWarnings("all")
public class NuoMiTest2 {

	public static WebDriver driver;

	static {
		// http://blog.csdn.net/aerchi/article/details/8055913
		// 设置浏览器启动环境变量
		// System.setProperty("webdriver.chrome.driver",
		// "D:\\DevSoft\\chromedriver.exe");
		System.setProperty("webdriver.ie.driver", "d:/Devsoft/IEDriverServer.exe");
		// driver = new ChromeDriver();
		DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
		ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
		ieCapabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,UnexpectedAlertBehaviour.ACCEPT);
		//InternetExplorerDriverService service = InternetExplorerDriverService.createDefaultService();
		driver = new InternetExplorerDriver(ieCapabilities); // 问题3解决方法
		driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS);// SECONDS是秒的意思
	}

	public static void main(String[] args) throws IOException, Exception {
        String formPath ="d:/pc_task/ysbj.txt";
		String resText = "",content="";
		//FileReader read = null;
		Reader read=null;
		BufferedReader reader = null;
		try {
			//read = new FileReader(new File(formPath));
			read = new InputStreamReader(new FileInputStream(formPath),"UTF-8");
			reader = new BufferedReader(read);
			content = reader.readLine();
			Map<String, String> map = new HashMap<String, String>();
			while (content != null) {
				 content = reader.readLine();
				 if(!"".equals(content)&& content!="null"){
					 //Thread.sleep(1000);
					 //driver.get(content);
					 driver.navigate().to(content);
					 if(isAlertPresent()){
						 driver.switchTo().alert().accept();
					 }
					 WebElement name = driver.findElement(By.className("branch-name"));
					 WebElement tel = driver.findElement(By.className("branch-tel"));
					 WebElement address = driver.findElement(By.className("branch-address"));
					 System.out.println(name.getText()+"="+tel.getText()+"="+address.getText());
				 }
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null)
					reader.close();
				if (read != null)
					read.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}


		// driver.navigate().refresh();
		driver.close();
		driver.quit();
	}

	protected static boolean isAlertPresent() {
        try {
          driver.switchTo().alert();
          return true;
        } catch (NoAlertPresentException e) {
          return false;
        }
      }
}